<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户编号" prop="userId">
        <el-select
          v-model="queryParams.userId"
          style="width: 240px"
          filterable
        >
          <el-option
            v-for="dict in userOptions"
            :key="dict.id"
            :label="dict.phone + ' --- ' + dict.userName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="装备编号" prop="gunsId">
        <el-select
          v-model="queryParams.gunsId"
          style="width: 240px"
          filterable
        >
          <el-option
            v-for="dict in gunsOptions"
            :key="dict.id"
            :label="dict.gunsName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="isStatus">
        <el-select
          v-model="queryParams.isStatus"
          style="width: 240px"
          filterable
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.val"
            :label="dict.label"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:guns_user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:guns_user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:guns_user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:guns_user:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="guns_userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="装备编号" align="center" prop="gunsId" />
      <el-table-column label="装备数量" align="center" prop="gunsCount" />
      <el-table-column label="装备状态" align="center" prop="isStatus" >
        <template slot-scope="scope">
          {{scope.row.isStatus == 1 ? '待出售' : scope.row.isStatus == 2 ? '出售' :'冻结' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:guns_user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:guns_user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改个人装备背包对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="装备编号" prop="gunsId">
          <el-input v-model="form.gunsId" placeholder="请输入装备编号" />
        </el-form-item>
        <el-form-item label="装备数量" prop="gunsCount">
          <el-input v-model="form.gunsCount" placeholder="请输入装备数量" />
        </el-form-item>

        <el-form-item label="状态" prop="isStatus">
          <el-select
            v-model="form.isStatus"
            style="width: 240px"
            filterable
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.val"
              :label="dict.label"
              :value="dict.val"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuns_user, getGuns_user, delGuns_user, addGuns_user, updateGuns_user, exportGuns_user } from "@/api/admin/guns_user";
import { listUser} from "@/api/admin/user";
import { listGuns } from "@/api/admin/guns";
export default {
  name: "Guns_user",
  data() {
    return {
      // 遮罩层
      loading: true,
      userOptions:[],
      gunsOptions:[],
      // 选中数组
      ids: [],
      statusOptions:[
        //1待出售 2出售了 3被冻结
        {
          label:"待出售",
          val:1
        },
        {
          label:"出售",
          val:2
        },
        {
          label:"冻结",
          val:3
        },
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人装备背包表格数据
      guns_userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,

        gunsId: null,

        gunsCount: null,

        isStatus: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {


      }
    };
  },
  created() {
    this.getList();
    listUser({pageNum: 1,pageSize: 100000}).then(res =>{
      this.userOptions = res.rows;
    })
    listGuns({pageNum: 1,pageSize: 100000}).then(res =>{
      this.gunsOptions = res.rows;
    });
  },
  methods: {
    /** 查询个人装备背包列表 */
    getList() {
      this.loading = true;
      listGuns_user(this.queryParams).then(response => {
        this.guns_userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        userId: null,

        gunsId: null,

        gunsCount: null,

        createTime: null,

        isStatus: 0,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个人装备背包";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGuns_user(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改个人装备背包";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGuns_user(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGuns_user(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除个人装备背包编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGuns_user(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有个人装备背包数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGuns_user(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
